package com.peppa.petshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peppa.petshop.entity.Category;
import com.peppa.petshop.entity.Good;
import com.peppa.petshop.mapper.CategoryMapper;
import com.peppa.petshop.mapper.GoodMapper;
import com.peppa.petshop.service.GoodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peppa.petshop.vo.DataVO;
import com.peppa.petshop.vo.GoodVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sound.midi.SoundbankResource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张三
 * @since 2021-06-02
 */
@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> implements GoodService {

    @Autowired(required = false)
    private GoodMapper  goodMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    @Override
    public DataVO<GoodVO> goodList(Integer page, Integer limit) {
        if (page== null){
            page=1;
            limit = 10;
        }
        IPage<Good> goodIPage = new Page<Good>(page,limit);
        IPage<Good> result = goodMapper.selectPage(goodIPage, null);
        List<Good> goods = result.getRecords();
        List<GoodVO> goodVOS = new ArrayList<>();
        Integer count = (int) result.getTotal();
        for (Good good : goods) {
            GoodVO vo = new GoodVO();
            System.out.println(vo.getImagePath());
            System.out.println(good.getImagePath());

            BeanUtils.copyProperties(good,vo);
            String imagePaths = good.getImagePath();
            String[] split = imagePaths.split(";");
            String s =split[0] ;
            vo.setImagePath(s);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("category_id",good.getCategory());
            vo.setCategory(categoryMapper.selectOne(queryWrapper).getCategory());
            goodVOS.add(vo);
        }
        DataVO<GoodVO> goodDataVO = new DataVO<GoodVO>(0,"",count,goodVOS);
        return goodDataVO;
    }

    @Override
    public void deleteById(Integer id) {
        goodMapper.deleteById(id);
    }

    @Override
    public synchronized void  addImagesPath(Integer goodId, String path) {
        Good good = goodMapper.selectById(goodId);
        String imagePath = good.getImagePath();
        System.out.println(path);
        good.toString();
        System.out.println("path:"+imagePath);
        if (imagePath!=null){
            path = imagePath+";"+path;
            good.setImagePath(path);
            System.out.println(path);
            goodMapper.updateById(good);
        }else {
            good.setImagePath(path);
            goodMapper.updateById(good);
        }
    }

    @Override
    public void addGood(Good good) {
        good.setCreateTime(LocalDateTime.now());
        good.setUpdateTime(LocalDateTime.now());
        goodMapper.insert(good);
    }

    @Override
    public void editById(Integer goodId, String goodName, BigDecimal price, String introduction) {
        Good good = goodMapper.selectById(goodId);
        good.setGoodName(goodName);
        good.setPrice(price);
        good.setIntroduction(introduction);
        good.setUpdateTime(LocalDateTime.now());
        goodMapper.updateById(good);
    }


}
